import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,ModalController ,LoadingController} from 'ionic-angular';
import {LoginPage} from '../login/login'
import {Storage} from '@ionic/storage'
import { BaseUI } from '../../commom/baseui';
import {RestProvider} from '../../providers/rest/rest';
import {UserPage} from '../../pages/user/user'
import {UserdatalistPage} from '../../pages/userdatalist/userdatalist'
import {SettingsProvider} from '../../providers/settings/settings'
import { ScanPage } from '../scan/scan';
import { VersionsPage } from '../versions/versions'
import { BaidumapPage} from '../baidumap/baidumap'
declare const baidumap_location: any

/**
 * Generated class for the MorePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-more',
  templateUrl: 'more.html',
})
export class MorePage extends BaseUI{

  public notLogin : boolean = true;
  public logined : boolean = false;
  public headface : string = "../../assets/imgs/placeholder.png"
  public userInfo: string[];
  public selectedTheme: string;
  public latitude: string;
  public lontitude: string;
  

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              public modalCtrl: ModalController,
              public storage:Storage,
              public loadingCtrl: LoadingController,
              public rest: RestProvider,
              public settings:SettingsProvider
              ) {
              super()
              this.settings.getActiveTheme().subscribe(val => this.selectedTheme = val);
  }

  showModal(){
    let modal = this.modalCtrl.create(LoginPage);
    //关闭后的回调
    modal.onDidDismiss(()=>{
      this.loadUserPage()
    });
    modal.present();

  }

  ionViewDidEnter(){
    this.loadUserPage();
  }

  loadUserPage(){
    this.storage.get('UserId').then((val)=>{
      if(val!=null){
        //加载用户数据
        var loading = super.showLoading(this.loadingCtrl,"加载中...");
        this.rest.getUserInfo(val)
        .subscribe(
          userInfo => {
            this.userInfo = userInfo;
            this.headface = userInfo["UserHeadface"] + "?" + (new Date).valueOf();
            this.notLogin = false;
            this.logined = true;
            loading.dismiss();
          }
        );
      }else{
        this.notLogin = true;
        this.logined = false;
      }
    });

  }

  gotoUserPage() {
    this.navCtrl.push(UserPage)

  }

  gotoDataList(type){
    this.navCtrl.push(UserdatalistPage,{"dataType" : type})
  }

  toggleChangeTheme(){
    
    if (this.selectedTheme === 'dark-theme'){
      this.settings.setActiveTheme('light-theme')
    }
    else{
      this.settings.setActiveTheme('dark-theme')
    }

  }

  /**
   * 跳转到扫描二维码的页面，加上 animate = false 的参数是为了
   * 相机能够在整个屏幕中显示，如果不加，相机就出不来。
   * animate 的参数默认值为 true
   * 
   * @memberof MorePage
   */
  gotoScanQRCode(){
    this.navCtrl.push(ScanPage,null,{"animate":false})

  }

  gotoVersions(){
    this.navCtrl.push(VersionsPage)

  }
  gotoBaiduMap(latitude,longitude){
    
    this.navCtrl.push(BaidumapPage,{latitude:latitude,longitude:longitude})
    
  }
  baidumaplocation(){ 
    
    if (typeof baidumap_location === "undefined") { 
        alert("baidumap_location is undefined"); 
        return; 
    }; 
    baidumap_location.getCurrentPosition(
      result=> { 
      //alert(JSON.stringify(result, null, 4)); 
      //console.log(result.latitude)
      var latitude=result.latitude;
      var longitude=result.longitude;
      this.gotoBaiduMap(latitude,longitude)
     }, function (error) { 
        alert(error); 
    });
  }

}
